#!/bin/bash

export PYAV_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.."; pwd)"

export PYAV_PYTHON_VERSION="2.7.13"
export PYAV_PLATFORM_SLUG="$(uname -s).$(uname -r)"
export PYAV_VENV_NAME="$PYAV_PLATFORM_SLUG.cpython-$PYAV_PYTHON_VERSION-debug"
export PYAV_VENV="$PYAV_ROOT/venvs/$PYAV_VENV_NAME"


export PYAV_PYTHON_SRC="$PYAV_ROOT/vendor/Python-$PYAV_PYTHON_VERSION"

if [[ ! -d "$PYAV_PYTHON_SRC" ]]; then
	url="https://www.python.org/ftp/python/$PYAV_PYTHON_VERSION/Python-$PYAV_PYTHON_VERSION.tgz"
	echo "Downloading $url"
	wget -O "$PYAV_PYTHON_SRC.tgz" "$url" || exit 2
	tar -C "$PYAV_ROOT/vendor" -xvzf "$PYAV_PYTHON_SRC.tgz" || exit 3
fi

cd "$PYAV_PYTHON_SRC" || exit 4

# TODO: Make generic.
export CPPFLAGS="-I$(brew --prefix openssl)/include"
export LDFLAGS="-L$(brew --prefix openssl)/lib"

# --with-pymalloc \
./configure \
	--with-pydebug \
	--prefix "$PYAV_VENV" \
	|| exit 5

make -j 12 || exit 6

